#pragma once
#include "DSXLoader.h"
using namespace std;



const float cellRadius = 0.5;

Comp* cell;
Comp* orb;
texture* water1;
texture* water2;
texture* water3;
texture* dirt;
obj_type* _camel;
obj_type* _man;
obj_type* _palmTree;
DSXLoader * _scene;
GLuint PalmTreeDL;

void readyComponents(DSXLoader * s, obj_type & c, obj_type & m, obj_type & pt)
{
	_scene = s;
	cell = &_scene->comp["cell"];
	orb = &_scene->comp["colororb"];
	water1 = &_scene->Textures["water1"];
	water2 = &_scene->Textures["water2"];
	water3 = &_scene->Textures["water3"];
	dirt = &_scene->Textures["dirt"];
	_camel=&c;
	_man=&m;
	_palmTree=&pt;
}

void applyMaterial(int index)
{
	orb->applyMaterial(index);
}


void render(obj_type * object){
	int l_index;


	glBegin(GL_TRIANGLES); // glBegin and glEnd delimit the vertices that define a primitive (in our case triangles)
	for (l_index=0;l_index<(*object).polygons_qty;l_index++)
	{
		//----------------- FIRST VERTEX -----------------
		// Coordinates of the first vertex
		glVertex3f( (*object).vertex[ (*object).polygon[l_index].a ].x,
					(*object).vertex[ (*object).polygon[l_index].a ].y,
					(*object).vertex[ (*object).polygon[l_index].a ].z); //Vertex definition

		//----------------- SECOND VERTEX -----------------
		// Coordinates of the second vertex
		//float x= object.vertex[ object.polygon[l_index].b ].x;

		glVertex3f( (*object).vertex[ (*object).polygon[l_index].b ].x,
					(*object).vertex[ (*object).polygon[l_index].b ].y,
					(*object).vertex[ (*object).polygon[l_index].b ].z);
        
		//----------------- THIRD VERTEX -----------------
		// Coordinates of the Third vertex
		glVertex3f( (*object).vertex[ (*object).polygon[l_index].c ].x,
					(*object).vertex[ (*object).polygon[l_index].c ].y,
					(*object).vertex[ (*object).polygon[l_index].c ].z);
	}
	glEnd();
	
}


void drawCamel(int color)
{
	glPushMatrix();
	applyMaterial(color-1);
	glTranslated(0,1.5,0);
	glRotated(-90,1,0,0);
	glScaled(0.0035,0.0035,0.0035);
	render(_camel);
	glPopMatrix();
}

void drawRider(int color)
{
	glPushMatrix();
	applyMaterial(color-1);
	glScaled(2,2,2);
	glRotated(-90,1,0,0);
	render(_man);
	glPopMatrix();
}

void drawPalmTree()
{
	glEnable(GL_COLOR_MATERIAL);
	glColor3f(0,0.5,0);
	glPushMatrix();
	GLUquadric* glQ = gluNewQuadric();

	glScaled(0.1,0.1,0.1);
	glRotated(-90,1,0,0);

	gluCylinder(glQ,0.2,0.2,8.0,20,20);
	glPushMatrix();
	glTranslated(0.2,-2.,-3.0);
	render(_palmTree);
	glPopMatrix();
	glPopMatrix();
	glDisable(GL_COLOR_MATERIAL);
}

void createPalmTreeDL() {
	PalmTreeDL = glGenLists(1);

	glNewList(PalmTreeDL,GL_COMPILE);
		
		drawPalmTree();
	glEndList();
}

void draw(string celltype)
{
	//Comp* cell= &_scene->comp["cell"];

	if( celltype.size() != 3 )
		switch(celltype[0])
		{
			case '1':
				cell->draw(NULL,water1);
				break;
			case '2':
				cell->draw(NULL,water2);
				break;
			case '3':
				cell->draw(NULL,water3);
				break;
			case 'o':
				cell->draw(NULL,dirt);
				glCallList(PalmTreeDL);
				break;
			default: 
				cell->draw();
				break;
		}	
	else
	{
		cell->draw();
		if(celltype[1]=='c')
		{
			drawCamel(atoi(&celltype[2]));
		}
		else
		if(celltype[1]=='r')
		{

			drawRider(atoi(&celltype[2]));
			/*
			glPushMatrix();
			glTranslated(0,0.5,0);
			applyMaterial(atoi(&celltype[2])-1);
			GLUquadric* glQ = gluNewQuadric();
			gluQuadricOrientation(glQ, GLU_OUTSIDE);
			gluSphere(glQ,0.5,10,10);
			glPopMatrix();
			*/
		}

	}
}

void drawBoard(vector< vector<string> > & tab, GLenum mode, int invx, int invz,float invsize,bool & inv)
{
	GLint pickingIndex = 1;
	float z=0;
	float step = 2* cellRadius;
	
	for(size_t i = 0; i < tab.size(); i++)
	{
		float x=0;
		
		if(i%2 != 0)
			x=0;
		else
			x=0.5;

		for(size_t j = 0; j < tab[i].size(); j++)
		{
			if(tab[i][j]!="0")
			{
				if (mode == GL_SELECT)
				{
					glLoadName (pickingIndex);
				}
				glPushMatrix();
				glTranslated(x,0,z);
				if(inv)
					if(j==invx && i== invz)
						glScaled(invsize,invsize,invsize);
				draw(tab[i][j]);
				glPopMatrix();
				
			}
			++pickingIndex;
			x+=step;
			
		}
		
		z+=step;
	}
}


void drawColorOptions(vector< vector<string> > & tab,int & selected, GLenum mode)
{
	glEnable(GL_COLOR);
	float step = tab.size()/5;
	float x = tab[0].size()+1;
	float z	= step/2;

	for(int i=1; i<= 5; ++i)
	{
		if (mode == GL_SELECT)
			glLoadName (1000+i);

		glPushMatrix();
		glTranslated(x,0.5,z);
		orb->setMatIndex(i-1);
		orb->draw();
		glPopMatrix();
		z+=step;
	}
	glDisable(GL_COLOR);
}
